package com.iterative.system.service;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iterative.common.persistence.HibernateDao;
import com.iterative.common.service.BaseService;
import com.iterative.system.dao.RolePermissionDao;
import com.iterative.system.entity.RolePermission;
import com.iterative.system.realm.UserRealm;
@Service
@Transactional(readOnly=true)
public class RolePermissionService extends BaseService<RolePermission, Long>{

	@Autowired
	private RolePermissionDao rolePermissionDao;
	
	@Override
	public HibernateDao<RolePermission, Long> getEntityDao() {
		return rolePermissionDao;
	}
	
	/**
	 * 获取角色权限id集合
	 * @param id
	 * @return List
	 */
	public List<Long> getPermissionIds(Long roleId) {
		return rolePermissionDao.findPermissionIds(roleId);
	}

	/**
	 * 清空该角色用户的权限缓存
	 */
	public void clearUserPermCache(PrincipalCollection pc){
		RealmSecurityManager securityManager =  (RealmSecurityManager) SecurityUtils.getSecurityManager();
		UserRealm userRealm = (UserRealm) securityManager.getRealms().iterator().next();
	    userRealm.clearCachedAuthorizationInfo(pc);
	}

	/**
	 * 修改角色权限
	 * @param id
	 * @param oldList
	 * @param newList
	 */
	@Transactional(readOnly = false)
	public void updateRolePermission(Long id, List<Long> oldList,
			List<Long> newList) {
		//是否删除
				for(int i=0,j=oldList.size();i<j;i++){
					if(!newList.contains(oldList.get(i))){
						rolePermissionDao.deleteRP(id,oldList.get(i));
					}
				}
				
				//是否添加
				for(int i=0,j=newList.size();i<j;i++){
					if(!oldList.contains(newList.get(i))){
						rolePermissionDao.save(getRolePermission(id,newList.get(i)));
					}
				}
	}

	/**
	 * 构造角色权限对象
	 * @param roleId
	 * @param permissionId
	 * @return RolePermission
	 */
	private RolePermission getRolePermission(Long roleId, Long permissionId) {
		RolePermission rp = new RolePermission();
		rp.setRoleId(roleId);
		rp.setPermissionId(permissionId);
		return rp;
	}

}
